home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / romize.zip / BINTOHEX.C next >
Text File  |  1988-07-07  |  1KB  |  69 lines

  1. /* Binary image to Intel HEX format */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <dos.h>
  7. #include <alloc.h>
  8.  
  9. #define true 1
  10. #define false 0
  11. #define romsize 32768
  12. #define lineincr  32     /* # bytes/line */
  13.  
  14.   unsigned char buffer[romsize];
  15.   FILE  *output;
  16.  
  17.   int binhandle;
  18.  
  19. int convert(void)
  20. {
  21.   unsigned i;
  22.   int k;
  23.   unsigned char *j;
  24.   unsigned cksum;
  25.   unsigned bytesread;
  26.  
  27.   bytesread = _read(binhandle,buffer,romsize);
  28.   for (i = 0; i < bytesread; i+=lineincr) {
  29.     j = &buffer[i];
  30.     fprintf(output,":%02X%04X00",lineincr,i);
  31.     cksum = lineincr + (i >>8) + (i & 0xff);
  32.     for (k = 0; k < lineincr; k++,j++) {
  33.       cksum += *j;
  34.       fprintf(output,"%02X",*j);
  35.     }
  36.     fprintf(output,"%02X\n",-(cksum & 0xff));
  37.   }
  38.   fprintf(output,":0000000000\n");
  39.   return(0);
  40. }
  41.  
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.   printf("Binary image to Intel HEX format\n");
  46.  
  47.   if (argc < 3) {
  48.     printf("Usage:  bintohex input.bin output.hex \n");
  49.     return (1);
  50.   }
  51.  
  52.   binhandle = _open(argv[1],O_RDONLY|O_BINARY);
  53.   if (binhandle < 0) {
  54.     printf("Trouble opening input file\n");
  55.     return(1);
  56.   }
  57.  
  58.   output = fopen(argv[2],"wt");
  59.   if (!output) {
  60.     printf("Trouble opening output\n");
  61.     return(1);
  62.   }
  63.   convert();
  64.  
  65.  
  66.   close(binhandle);
  67.   fclose(output);
  68.   return(0);
  69. }